package com.github.prontera.controller;


import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.common.filter.CurrentUserId;
import com.github.prontera.domain.Client;
import com.github.prontera.domain.ClientAccount;
import com.github.prontera.domain.ClientInvitation;
import com.github.prontera.domain.Consume;
import com.github.prontera.domain.ZClient;
import com.github.prontera.domain.enums.ConsumeLevelTypeEnum;
import com.github.prontera.domain.enums.ConsumeSmallTypeEnum;
import com.github.prontera.domain.enums.RegisterChannelEnum;
import com.github.prontera.domain.enums.UserStatusEnum;
import com.github.prontera.domain.enums.VipStatusEnum;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.model.response.Response;
import com.github.prontera.service.ClientAccountService;
import com.github.prontera.service.ClientInvitationService;
import com.github.prontera.service.ClientService;
import com.github.prontera.service.ConsumeService;
import com.github.prontera.service.ZClientService;
import com.github.prontera.util.Page;
import com.github.prontera.util.RandomStringGenerator;

import io.swagger.annotations.ApiOperation;

/**
 * 用户管理
 * @author Mengxiangdong
 */

@RestController
@RequestMapping(value = ApiVersion.MANAGE_V1_PREFIX + "/zclient", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, consumes = { MediaType.APPLICATION_JSON_UTF8_VALUE })
public class ZClientController {
	
	@Autowired
	private ZClientService zclientService;
	@Autowired
	private ClientService clientService;
	@Autowired
	private ClientInvitationService clientInvitationService;
	@Autowired
	private ConsumeService consumeService;
	@Autowired
	private ClientAccountService clientAccountService;

	@ApiOperation(value = "上线", notes = "查询用户详情")
	@RequestMapping(value = "/pushClient", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<ZClient> pushClient(@RequestBody ZClient client) {
		client= zclientService.get(client.getId());
		
		client.setStatus(UserStatusEnum.DISABLE);
		client.setHasHidden(0);
		zclientService.update(client);
		
		Client client2=new Client();
		BeanUtils.copyProperties(client, client2);
		client2.setHasHidden(1);//隐藏
		client2.setVipStatus(VipStatusEnum.VIP);
		client2.setVipAmountId(12l);
		client2.setLastVipTime(OffsetDateTime.now());
		client2.setLastVipExpireTime(OffsetDateTime.now().plusYears(1));
		
		int x1=121;
		int x2=31;
		
        int s1 = new Random().nextInt(x1)%(x1-121+1) + 121;
        int s2 = new Random().nextInt(x2)%(x2-1+1) +1;
		
		client2.setPointLog(Double.parseDouble(x1+"."+s1));
		client2.setPointLat(Double.parseDouble(x2+"."+s2));
		
		client2.setWechat(new RandomStringGenerator().getNewString());
		
		clientService.pushClient(client2);
		
		return new ObjectDataResponse<ZClient>(client);
	}
	
	@ApiOperation(value = "查询用户列表", notes = "查询用户列表")
	@RequestMapping(value = "/queryClient", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<ZClient>> queryRole(@RequestBody ZClient client) {
		client.setOrderBy("U_CLIENT.create_time desc");
		Page<ZClient> datas = zclientService.page(client);
		return new ObjectDataResponse<Page<ZClient>>(datas);
	}
	
	@ApiOperation(value = "查询用户详情", notes = "查询用户详情")
	@RequestMapping(value = "/getClient", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<ZClient> getClient(@RequestBody ZClient client) {
		client= zclientService.get(client.getId());
		return new ObjectDataResponse<ZClient>(client);
	}
	
	@ApiOperation(value = "新增用户列表", notes = "新增用户列表")
	@RequestMapping(value = "/addClient", method = RequestMethod.POST)
	@ResponseBody
	public Response addClient(@RequestBody ZClient client) {
		
		client.setStatus(UserStatusEnum.NOMAIL);
		client.setRegisterChannel(RegisterChannelEnum.PC);
		int success=zclientService.insert(client);
		return new ObjectDataResponse<Integer>(success);
	}
	
	@ApiOperation(value = "删除用户", notes = "删除用户")
	@RequestMapping(value = "/deleteClient", method = RequestMethod.POST)
	@ResponseBody
	public Response deleteClient(@CurrentUserId Long userId, @RequestBody ZClient client) {
		int success = zclientService.remove(client);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", success);
		if(success == 2){
			map.put("msg", "该角色被用户所使用，无法被删除！");	
		}
		return new ObjectDataResponse<Object>(map);
	}
	
	
	@ApiOperation(value = "查询用户上下级关系", notes = "查询用户上下级关系")
	@RequestMapping(value = "/queryPassive", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<ClientInvitation>> queryPassive(@RequestBody ClientInvitation clientInvitation) {
		
		Page<ClientInvitation> datas = clientInvitationService.invitationPage(clientInvitation);
		return new ObjectDataResponse<Page<ClientInvitation>>(datas);
	}
	
	@ApiOperation(value = "实名认证", notes = "实名认证")
	@RequestMapping(value = "/updateClient", method = RequestMethod.POST)
	@ResponseBody
	public Response updateClient(@RequestBody ZClient client) {
		client.setRealTime(OffsetDateTime.now());
		int success = zclientService.update(client);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", success);
		if(success == 2){
			map.put("msg", "实名认证失败！");	
		}
		return new ObjectDataResponse<Object>(map);
	}
	
	@ApiOperation(value = "余额提现", notes = "余额提现")
	@RequestMapping(value = "/withdrawals", method = RequestMethod.POST)
	@ResponseBody
	public Response withdrawals(@RequestBody ZClient client) {
		client.setAmount(client.getAmount().subtract(client.getLocakedCashAmount()));
		int success = zclientService.update(client);
		if(success == 1){
			Consume consume=new Consume();
			consume.setClientId(client.getId());
			consume.setLevelType(ConsumeLevelTypeEnum.BALANCE);
			consume.setAmount(client.getLocakedCashAmount());
			consume.setStatus(2);
			consume.setConsumeType(ConsumeSmallTypeEnum.DEPOSIT);
			consumeService.insert(consume);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", success);
		if(success == 2){
			map.put("msg", "提现申请失败！");	
		}
		return new ObjectDataResponse<Object>(map);
	}
	

	@ApiOperation(value = "查询帐户列表", notes = "查询帐户列表")
	@RequestMapping(value = "/queryAccount", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<ClientAccount>> queryAccount(@RequestBody ClientAccount clientAccount) {

		clientAccount.setOrderBy("U_CLIENT_ACCOUNT.create_time desc");
		Page<ClientAccount> datas = clientAccountService.page(clientAccount);
		return new ObjectDataResponse<Page<ClientAccount>>(datas);
	}

}
